package com.cgbl.service.ui

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.TeamMemberAdapter
import com.cgbl.service.databinding.AppActivityEditTagBinding
import com.cgbl.service.vm.EditTagViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.MemberAdapterBean
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.arouter.ARouterUtil

@Route(path = AppConstants.Router.Project.A_EDIT_TAG)
class EditTagActivity : GLBaseActivity<AppActivityEditTagBinding, EditTagViewModel>() {

    private var id = ""

    lateinit var teamMemberAdapter: TeamMemberAdapter

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_edit_tag

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        teamMemberAdapter = TeamMemberAdapter()
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = teamMemberAdapter
        }
        teamMemberAdapter.setNewInstance(mutableListOf(MemberAdapterBean()))
        teamMemberAdapter.setOnItemClickListener { adapter, view, position ->
            ARouterUtil.goToActivityWithBundle(
                AppConstants.Router.Project.A_SELECT_MEMBER,
                Bundle().apply {
                    putStringArrayList(AppConstants.BundleKey.IDS, viewModel.memberIdList)
                })
        }
        binding.etTitle.addTextChangedListener(onTextChanged = { text, start, before, count ->
            viewModel.saveEnableField.set(!TextUtils.isEmpty(text) && viewModel.memberIdList.isNotEmpty())
        })
        viewModel.fetchLabelProfile(id)
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView?.text = if (TextUtils.isEmpty(id)) "创建标签" else "编辑标签"
        binding.tvBottomBtn.text = if (TextUtils.isEmpty(id)) "创建标签" else "删除标签"
        binding.tvRightText.visibility = if (TextUtils.isEmpty(id)) View.GONE else View.VISIBLE
        backView?.setOnClickListener { finish() }
    }


    override fun initParam() {
        id = intent.getStringExtra(AppConstants.BundleKey.ID) ?: ""
    }

    override fun initViewObservable() {
        viewModel.uc.labelMemberEvent.observe(this) {
            teamMemberAdapter.setNewInstance(it)
            teamMemberAdapter.addData(MemberAdapterBean())
            viewModel.memberIdList.clear()
            teamMemberAdapter.data.forEach { member ->
                if (!TextUtils.isEmpty(member.id)) {
                    viewModel.memberIdList.add(member.id ?: "")
                }
            }
            viewModel.saveEnableField.set(!TextUtils.isEmpty(binding.etTitle.text.toString()) && viewModel.memberIdList.isNotEmpty())


        }
        LiveDataEventManager.observerSelectTagMemberEvent(this) {
            teamMemberAdapter.setNewInstance(it as MutableList<MemberAdapterBean>?)
            teamMemberAdapter.addData(MemberAdapterBean())
            viewModel.memberIdList.clear()
            teamMemberAdapter.data.forEach { member ->
                if (!TextUtils.isEmpty(member.id)) {
                    viewModel.memberIdList.add(member.id ?: "")
                }
            }
            viewModel.saveEnableField.set(!TextUtils.isEmpty(binding.etTitle.text.toString()) && viewModel.memberIdList.isNotEmpty())
        }
    }
}